<?php

namespace app\api\controller\web\frontend;

use app\common\controller\Api;
use think\facade\Db;
/**
 * 标签管理
 */
class Label extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 通用的列表
     */
    public function get_label()
    {

        // 根据团队筛选
        $school = input('school', '');
        $realm = input('realm', '');
        $team = input('team', '');

        $where = [];
        if ($school) {
            $where[] = ['school', '=', $school];
        }
        if ($realm) {
            $where[] = ['realm', '=', $realm];
        }
        if ($team) {
            $where[] = ['id', '=', $team];
        }

        $teams = Db::name('web_excellent_team')->field('realm, school')->where($where)->order('myorder desc')->select()->toArray();

        $type = input('type') ?? 1;
        $where = [];
        if ($type == 1) {
            $realms = array_column($teams, 'realm');
            if ($realms) {
                $where[] = ['id', 'in', $realms];
                $data = Db::name('web_label')->where('type', $type)->where($where)->order('myorder desc')->select()->toArray();
            }
        }else{
            $schools = array_column($teams, 'school');
            if ($schools) {
                $where[] = ['id', 'in', $schools];
                $data = Db::name('web_label')->where('type', $type)->where($where)->order('myorder desc')->select()->toArray();
            }
        }
        

        $is_all = input('is_all', '');
        if ($is_all) {
            $all = ['id'=>'', 'name'=>'全部'];
            array_unshift($data, $all);
        }

        return ajaxReturn('success', 1, $data);
    }

    public function list()
    {
        // 分类
        $type = input('type') ?? 1;
        $keywords = input('keywords') ?? '';
        $where = [];
        if ($type) {
            $where[] = ['state', '=', $type];
        }
        if ($keywords) {
            $where[] = ['name', 'like', '%'.$keywords.'%'];
        }

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;
        
        $list = Db::name('web_label')
                        ->field('id, name, state, weigh')
                        ->where($where)
                        ->order('weigh desc')
                        ->limit($start, $page_size)
                        ->select()
                        ->toArray();
        
        $count = Db::name('web_label')->where($where)->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    /**
     * 列表详情
     */
    public function info()
    {
        $id = input('label_id');
        if (!$id) {
            return ajaxReturn('请求参数出错', 0);
        }

        $data = Db::name('web_label')->field('id, name, state, weigh')->find($id);
        if (empty($data)) {
            return ajaxReturn('不存在的标签', 0);
        }

        $this->success('success',$data);

    }


}
